package com.cdu.controller;

import com.cdu.annotation.OperationLogAnnotation;
import com.cdu.enums.MessageEnum;
import com.cdu.enums.OperationLogEnum;
import com.cdu.model.common.BaseResponse;
import com.cdu.model.common.PageResult;
import com.cdu.model.common.StringConst;
import com.cdu.model.query.UserQuery;
import com.cdu.pojo.User;
import com.cdu.service.UserService;
import com.cdu.util.ParamUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * projectName : easy-help-v2
 * className: UserController
 * description:
 *
 * @author : chengjiang@jasonch.tech
 * @date: 2022/05/10
 */

@RestController
@RequestMapping("/u")
@Api(value = "用户controller", tags = "userAPI")
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/login")
    @ApiOperation("登录接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.USER_LOGIN)
    public BaseResponse login(HttpServletRequest request) {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return userService.login(ido);
    }

    @PostMapping("/register")
    @ApiOperation("注册接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.USER_LOGIN)
    public BaseResponse register(HttpServletRequest request) {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return userService.register(ido);
    }

    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "查询用户id", paramType = "path", dataType = "String", required = true, dataTypeClass = String.class)
    )
    @GetMapping({"/info/{id}", "/info"})
    @ApiOperation("根据id查询用户详情")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY_USER_DETAIL)
    public BaseResponse detail(HttpServletRequest request, @PathVariable(required = false) String id) {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        if (!StringUtils.hasLength(id)) {
            id = (String) request.getAttribute("userId");
        }
        return BaseResponse.response(userService.getById(id), MessageEnum.SUCCESS);
    }


    @GetMapping("/list")
    @ApiOperation("查询所有任务")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse list(HttpServletRequest request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        log.info("查询所有任务: {}", this.getClass());
        var pageInfo = PageInfo.of(userService.list());
        return BaseResponse.response(PageResult.doPage(
                pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()
        ), MessageEnum.SUCCESS);
    }

    @PostMapping("/{id}")
    @ApiOperation("修改用户接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.OTHER)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "修改用户id", paramType = "path", dataType = "String", required = true, dataTypeClass = String.class)
    )
    public BaseResponse update(HttpServletRequest request, @PathVariable(required = false) String id) {
        log.info("修改用户接口: {}", this.getClass());
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        if (!StringUtils.hasLength(id)) {
            id = (String) request.getAttribute("userId");
        }
        User user = userService.getById(id);
        if (null == user) {
            return BaseResponse.response(MessageEnum.ERROR);
        }
        user.setId(id);
        user.setBedroomAddr(ido.getString("bedroomAddr"));
        user.setQq(ido.getString("qq"));
        user.setRealName(ido.getString("realName"));
        user.setRemark(ido.getString("remark"));
        user.setReputation(100);
        user.setRole(StringConst.ROLE_USER);
        user.setTel(ido.getString("tel"));
        user.setUsername(ido.getString("username"));
        return BaseResponse.response(userService.updateById(user), MessageEnum.SUCCESS);
    }

    @GetMapping("/del/{id}")
    @ApiOperation("通过id删除指定用户")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.DELETE)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "删除用户id", paramType = "path", dataType = "String", required = true, dataTypeClass = String.class)
    )
    public BaseResponse del(HttpServletRequest request, @NotBlank(message = "id不能为空") @PathVariable String id) {
        log.info("您删除的id是: {}", id);
        return BaseResponse.response(userService.removeById(id), MessageEnum.SUCCESS);
    }

    @PostMapping("/search")
    @ApiOperation("查询所有任务")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.OTHER)
    public BaseResponse list(HttpServletRequest request, UserQuery userQuery) {
        return BaseResponse.response(userService.queryUser(userQuery), MessageEnum.SUCCESS);
    }

}
